package com.hanthink.web.controller.pc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.pc.EmTaskPlan;
import com.hanthink.system.service.pc.IEmTaskPlanService;

/**
 * 任务计划Controller
 * 
 * @author hanthink
 * @date 2021-01-20
 */
@RestController
@RequestMapping("/equipment/PLAN")
public class EmTaskPlanController extends BaseController {
	@Autowired
	private IEmTaskPlanService emTaskPlanService;

	@Autowired
	private TokenService tokenService;

	/**
	 * 查询任务计划列表
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:list')")
	@GetMapping("/list")
	public TableDataInfo list(EmTaskPlan emTaskPlan) {
		startPage();
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			emTaskPlan.setSiteKey(loginUser.getUser().getSite().toString());
		}
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
			emTaskPlan.setHangameKey(loginUser.getUser().getHangame().toString());
		}
		List<EmTaskPlan> list = emTaskPlanService.selectEmTaskPlanList(emTaskPlan);
		return getDataTable(list);
	}

	/**
	 * 导出任务计划列表
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:export')")
	@Log(title = "任务计划", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(EmTaskPlan emTaskPlan) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L) {
			emTaskPlan.setSiteKey(loginUser.getUser().getSite().toString());
		}
		List<EmTaskPlan> list = emTaskPlanService.selectEmTaskPlanList(emTaskPlan);
		ExcelUtil<EmTaskPlan> util = new ExcelUtil<EmTaskPlan>(EmTaskPlan.class);
		return util.exportExcel(list, "PLAN");
	}

	/**
	 * 获取任务计划详细信息
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:query')")
	@GetMapping(value = "/{siteKey}")
	public AjaxResult getInfo(@PathVariable("siteKey") String siteKey) {
		return AjaxResult.success(emTaskPlanService.selectEmTaskPlanById(siteKey));
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/data")
	public AjaxResult getSiteList() {
		return AjaxResult.success(emTaskPlanService.getSiteList());
	}

	@Log(title = "计划管理", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:import')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<EmTaskPlan> util = new ExcelUtil<EmTaskPlan>(EmTaskPlan.class);
		List<EmTaskPlan> planList = util.importExcel(file.getInputStream());
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		String operName = loginUser.getUsername();
		String message = emTaskPlanService.importPlan(planList, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate() {
		ExcelUtil<EmTaskPlan> util = new ExcelUtil<EmTaskPlan>(EmTaskPlan.class);
		return util.importTemplateExcel("计划数据");
	}

	/**
	 * 新增任务计划
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:add')")
	@Log(title = "任务计划", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody EmTaskPlan emTaskPlan) {
		return toAjax(emTaskPlanService.insertEmTaskPlan(emTaskPlan));
	}

	/**
	 * 修改任务计划
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:edit')")
	@Log(title = "任务计划", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody EmTaskPlan emTaskPlan) {
		return toAjax(emTaskPlanService.updateEmTaskPlan(emTaskPlan));
	}

	/**
	 * 删除任务计划
	 */
	@PreAuthorize("@ss.hasPermi('equipment:PLAN:remove')")
	@Log(title = "任务计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{siteKeys}")
	public AjaxResult remove(@PathVariable String[] siteKeys) {
		return toAjax(emTaskPlanService.deleteEmTaskPlanByIds(siteKeys));
	}
}
